// +---------------------
// | 配置注册
// | https://githubim.com/sdk/jssdk/datasource.html#%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E6%95%B0%E6%8D%AE%E6%BA%90
// +---------------------

import { MessageTask, Message, Channel, SyncOptions } from "wukongimjssdk";
import { MediaMessageUploadTask } from "./provider-upload";
import { SyncMessagesTask } from "./provider-sync-messages";

// 配置注册
export default {
  init: (WKSDK: any) => {
    // 注册上传任务
    WKSDK.shared().config.provider.messageUploadTaskCallback = (message: Message): MessageTask => {
      return new MediaMessageUploadTask(message);
    };

    // 注册同步频道消息数据源
    WKSDK.shared().config.provider.syncMessagesCallback = async function (
      channel: Channel,
      opts: SyncOptions
    ): Promise<Message[]> {
      // 后端提供的获取频道消息列表的接口数据 然后构建成 Message对象数组返回
      let messages: Message[] = [];
      messages = await new SyncMessagesTask().start(channel, opts);
      console.log(" ✔ 最近消息同步 syncMessagesCallback >> messages", messages.length);
      return messages;
    };

    console.warn(" ✔ 注册数据源配置");
  },
};
